//
//  ViewController.swift
//  MYModule
//
//  Created by 李然 on 2022/7/19.
//

import UIKit
import NSObject_Rx
import EachNavigationBar

open class ViewController: UIViewController {
    
    /// 默认电池栏显示
    var statusBarStyle: UIStatusBarStyle = .default {
        
        didSet {
                                             
            setNeedsStatusBarAppearanceUpdate()
            
        }
        
    }
    
    deinit {
        print("\(self): 内存正常释放")
        print(#function)

    }

    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        setInterfaceStyle()
        
        makeBackItem()
        
        makeUI()
        
        bindViewModel()
        
    }
    
  
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        updateUI()
        
    }
    
    func setInterfaceStyle() {
//        
//        if #available(iOS 13.0, *) {
//            self.overrideUserInterfaceStyle = .dark
//        }
//        
        
        
    }
    
    func makeBackItem() {
        
        
        self.navigation.item.leftBarButtonItem = BarButtonItem(image: UIImage(named: "ic_back"), style: .plain, target: self, action: #selector(backAction))

    }
    
    @objc open func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
    
    open func makeUI() {
        self.view.backgroundColor = UIColor.white
        
        // 解决右滑返回失效问题
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        
        self.view.widgetId = "view_root"
    }

    open func updateUI() {
        
    }
    
    open func bindViewModel(){
        
    }
    
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
     
        return statusBarStyle
        
    }
    
    
    open override var shouldAutorotate: Bool {
        
        return false
        
    }
    
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        
        return .portrait
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        
        return .portrait
    }
    
    
    
}

extension ViewController:UIGestureRecognizerDelegate {
    
    //返回true表示所有相同类型的手势辨认都会得到处理
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:
        UIGestureRecognizer) -> Bool {
        return true
    }
    
    //是否允许手势
    open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer) {
            //只有二级以及以下的页面允许手势返回
            return (self.navigationController?.viewControllers.count)! > 1
        }
        return true
    }
}
